# (1) 定义了一个菜单条目类
class Entry:
    def __init__(self):
        self.no = ''
        self.text = ''


class MenuManager:
    def __init__(self):
        # print('构造函数')
        # (2) 存储菜单数据
        e1 = Entry()
        e1.no = '1'
        e1.text = '增加学生信息'

        e2 = Entry()
        e2.no = '2'
        e2.text = '删除学生信息'

        e3 = Entry()
        e3.no = '3'
        e3.text = '修改学生信息'

        e4 = Entry()
        e4.no = '4'
        e4.text = '查询学生信息'

        e5 = Entry()
        e5.no = '5'
        e5.text = '查询所有学生'

        e6 = Entry()
        e6.no = '6'
        e6.text = '排序'

        e0 = Entry()
        e0.no = '0'
        e0.text = '退出'

        self.menu = []  # 为什么不把e0-e6做成成员变量？
        self.menu.append(e1)
        self.menu.append(e2)
        self.menu.append(e3)
        self.menu.append(e4)
        self.menu.append(e5)
        self.menu.append(e6)
        self.menu.append(e0)

    def print_menu(self):
        # (3) 打印菜单
        print('*****菜单开始*****')

        for item in self.menu:
            print(item.no, item.text)

        print('*****菜单结束*****')


class StudentInfoSystem:

    def __init__(self):
        # 成员变量
        self.students = []

        # 打印所有的学生信息
        s1 = ['张三', 18, '男']
        s2 = ['李四', 12, '女']

        self.students.append(s1)
        self.students.append(s2)

    def execute(self):
        print('系统正在运行...')

        menu = MenuManager()

        while True:
            menu.print_menu()
            n = input('请选择菜单：')
            if n == '0':
                break
            elif n == '1':
                # print('1')
                self.save()

            elif n == '2':
                # print('2')
                self.remove()
            elif n == '3':
                # print('3')
                self.update()
            elif n == '4':
                # print('4')
                self.find_by_name()

            elif n == '5':
                self.find_all()
                # print('5')

            elif n == '6':
                self.sort()
                # print('6')
            else:
                print('无效的选项！')

    # 处理选项1
    def save(self):
        print('增加')

        s = []
        name = input('请输入姓名: ')
        age = input('请输入年龄: ')
        gender = input('请输入性别: ')
        s.append(name)
        s.append(age)
        s.append(gender)

        print(s)

        self.students.append(s)

    def remove(self):
        print('删除')
        n = input('请输入待删除的学生下标: ')

        if n.isdigit() == False:
            print('下标只能是数字！')

        elif int(n) < 0 or int(n) >= len(self.students):
            print('下标越界！')

        else:
            self.students.pop(int(n))

    def update(self):
        print('修改')

    def find_by_name(self):
        print('根据姓名查询')

        name = input('请输入姓名: ')

        for item in self.students:
            if name == item[0]:
                print(item)

    # 处理选项5
    def find_all(self):
        print('查询所有')

        for item in self.students:
            print('姓名: ', item[0],
                  '年龄: ', item[1],
                  '性别: ', item[2])

    def sort(self):
        print('排序')


# 构造一个学生信息系统对象
system = StudentInfoSystem()  # 默认执行一次init函数
system.execute()  # ctrl + b
